package com.ph.util.dbbackup;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public abstract class DBBackUpEXE {
	public abstract boolean exportDataBase() throws Throwable;

	public abstract String getExecuteBackupCommand();

	public abstract DBBackUpEXE createEXE();

	public void exe() {
		DBBackUpEXE dbbackup = createEXE();
		try {
			dbbackup.exportDataBase();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	/**
	 * 执行命令（使用cmd或者shell）
	 * 
	 * @param sCommand
	 * @return
	 */
	public static String execCommand(String sCommand) throws Throwable {
		String sResult = "", sErrorResult = "";
		try {
			Runtime run = Runtime.getRuntime();
			Process process = run.exec(sCommand);
			BufferedReader input = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			BufferedReader error = new BufferedReader(new InputStreamReader(
					process.getErrorStream()));
			String line = null;
			while ((line = input.readLine()) != null) {
				sResult += line + "\n";
			}
			String errorLine = null;
			while ((errorLine = error.readLine()) != null) {
				sErrorResult += errorLine + "\n";
			}
			if (sErrorResult != null && sErrorResult.startsWith("ERROR")) {
				sResult = sErrorResult;
			}
			input.close();
			error.close();
		} catch (Exception e) {
			e.printStackTrace();

		}
		return sResult;
	}
}
